﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using _12306Demo.Service;

namespace _12306Demo.UI
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            Load += FrmMain_Load;
        }

        async void FrmMain_Load(object sender, EventArgs e)
        {
            await InitServiceContext();
            InitStatusBar();
            InitQueryParamEdit();
        }

        #region 状态栏和工具栏事件
        /// <summary>
        /// 初始化状态栏
        /// </summary>
        private void InitStatusBar()
        {
            foreach (var lableItem in st.Items.OfType<ToolStripStatusLabel>().Where(s => s.IsLink && s.Tag != null))
            {
                lableItem.Click += (s, e) =>
                {
                    try
                    {
                        var toolStripStatusLabel = s as ToolStripStatusLabel;
                        if (toolStripStatusLabel != null)
                            Process.Start(toolStripStatusLabel.Tag.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, "错误：无法打开网址，错误信息：" + ex.Message + "。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }

                };
            }
        }

        /// <summary>
        /// 表示开始一个操作
        /// </summary>
        /// <param name="opName">当前操作的名字</param>
        /// <param name="maxItemsCount">当前操作如果需要显示进度，那么提供任务总数；不提供则为跑马灯等待</param>
        /// <param name="disableForm"></param>
        void BeginOperation(string opName, int maxItemsCount = 100, bool disableForm = false)
        {
            stStatus.Text = opName.DefaultForEmpty("正在操作，请稍等...");
            stProgress.Visible = true;
            stProgress.Minimum = maxItemsCount > 0 ? maxItemsCount : 100;
            stProgress.Style = maxItemsCount > 0 ? ProgressBarStyle.Blocks : ProgressBarStyle.Continuous;
            if (disableForm)
            {
                Enabled = false;
            }
        }
        /// <summary>
        /// 操作结束
        /// </summary>
        void EndOperation()
        {
            stStatus.Text = "就绪.";
            stProgress.Visible = false;
            Enabled = true;
        }

        /// <summary>
        /// 初始化查询参数编辑
        /// </summary>
        private void InitQueryParamEdit()
        {
            dtDate.MinDate = DateTime.Now;
            //dtDate.MaxDate = DateTime.Now.AddDays(60);
            //dtDate.Value = DateTime.Now.AddDays(1);
            dtDate.MaxDate = DateTime.Now.AddDays(_context.DataService.MaxSellDays);
            dtDate.Value = DateTime.Now.AddDays(_context.DataService.DefaultDayOffset);

            var allstationText = _context.StationDataService.AllStations.Select(s => (object)(s.FirstLetter.PadRight(5, ' ') + s.Name + "\t" + s.Code)).ToArray();
            cbFrom.Items.AddRange(allstationText);
            cbTo.Items.AddRange(allstationText);
        }
        #endregion

        #region 服务接入

        ServiceContext _context;
        /// <summary>
        /// 初始化服务
        /// </summary>
        async Task InitServiceContext()
        {
            _context = new ServiceContext();
            BeginOperation("", 0, true);
            await _context.StationDataService.LoadStationDatasAsync();
            EndOperation();
        }

        #endregion
    }
}
